package com.hhs.storage.util;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * A String-related utility
 * <BR>
 * <PRE>
 * <B>Revision History : </B>
 *	<!-- ------------------------------------ START OF Revision History -->
 *	2005/11/01 New making
 *  2006/01/10 safeTrim added by F.Bai
 *	<!-- ------------------------------------ END OF Revision History -->
 * </PRE>
 */
public class StringUtil {
	static public String changeBlank(String str) {
		str.replaceAll("\\n", "&n");
		str.replaceAll("\\r", "&r");
		return str.replaceAll("\\t", "&t");
	}
	/**
	 * 去除回车换行符
	 * @param str
	 * @return
	 */
	  public static String replaceBlank(String str) {
	        String dest = "";
	        if (str!=null) {
	            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
	            Matcher m = p.matcher(str);
	            dest = m.replaceAll("");
	        }
	        return dest;
	    }
	/** Conversion table */
	private static Hashtable	z2hTable	= null;

	/**
	 * The contents of the appointed character string are changed into to from "from".
	 * ex：StringUtil.changeStr("4,000", ",", "")
	 *                    → 4000 is return
	 *     ("4,000", ",", ",,") It lapses into an infinite loop.
	 * @param 	str Conversion character string
	 * @param	from Front [ conversion ] character string
	 * @param	to After [ conversion ] character string
	 * @return After [ conversion ] character string
	 */
	public static String changeStr(String str, String from, String to) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(str);
		while (str.indexOf(from) != -1) {
			sbf.replace(str.indexOf(from), str.indexOf(from) + from.length(), to);
			str = sbf.toString();
		}
		return sbf.toString();
	}

	/**
	 * Wavy line problem correspondence
	 * @param str_org	The character string for conversion
	 * @return After [ conversion ] character string
	 */
	public static String chgWaveDash(String str_org) {
		StringBuffer sbuf_org = new StringBuffer(str_org);
		int i = 0;
		while (i >= 0) {
			i = str_org.indexOf("\u301c", i);
			if (i > -1) {
				sbuf_org.replace(i, i + 1, "\uff5e");
				str_org = sbuf_org.toString();
				i++;
			}
		}
		return sbuf_org.toString();
	}

	/**
	 * "'" → "''" is alike and changes, From the appointed character string
	 * ex：StringUtil.deleteComma("'AAA'")
	 *                    → "''AAA''" 
	 * @param str	The character string for conversion
	 * @return String	After [ conversion ] character string
	 */
	public static String quoteAddQuote(String str) {
		StringBuffer sbf = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			if (str.substring(i, i + 1).equals("'")) {
				sbf.append("''");
			} else {
				sbf.append(str.substring(i, i + 1));
			}
		}
		return sbf.toString();
	}

	/**
	 * The character corresponding to un-[ HTML ] is changed.
	 * @param s	The character string for conversion
	 * @return After [ conversion ] character string
	 */
	public static String normalizeHTML(String s) {
		StringBuffer str = new StringBuffer();
		int len;

		if (s != null) {
			len = s.length();
		} else {
			len = 0;
		}
		for (int i = 0; i < len; i++) {
			char chr = s.charAt(i);
			switch (chr) {
				case '<':
					str.append("&lt;");
					break;
				case '>':
					str.append("&gt;");
					break;
				case '&':
					str.append("&amp;");
					break;
				case '"':
					str.append("&quot;");
					break;
				case '\t':
					str.append("&#9;");
					break;
				case ' ':
					str.append("&#32;");
					break;
				case '\r':
				case '\n':
					//                str.append("&#");
					//                str.append(Integer.toString(chr));
					//                str.append(';');
					str.append("<BR>");
					break;
				default:
					str.append(chr);
			}
		}
		return str.toString();
	}

	/**
	 * Cutoff of a character string
	 * @param str Character string
	 * @param len Length
	 * @return Character string
	 */
	protected String substring(String str, int len) {
		String temp = str;
		String out = "";
		int cnt = 0;
		for (int ii = 0; ii < temp.length(); ii++) {
			if (cnt >= len) {
				break;
			}
			int code = (int) temp.charAt(ii);

			if (255 < code) {
				cnt = cnt + 2;
			} else {
				cnt++;
			}
			out = out + temp.charAt(ii);
		}
		return out;
	}

	/**
	 * zNull
	 * @param str	Character string
	 * @return After [ String ] edit
	 */
	public static String zNull(String str) {
		if (str == null) {
			return "";
		}
		if (str.equals("null")) {
			return "";
		}
		if (str.equals("null|null")) {
			return "";
		}
		return str;
	}

	/**
	 * zNull
	 * @param str	Object
	 * @return After [ String ] edit
	 */
	public static String zNull(Object str) {
		if (str == null) {
			return "";
		}
		return zNull(str.toString());

	}

	/**
	 * setTDString
	 * In null or "",   is returned.
	 * @param str	The character string to verify
	 * @return After [ String ] edit
	 */
	static public String setTDString(String str) {
		if (str == null) {
			return "&nbsp;";
		}
		if (str.equals("")) {
			return "&nbsp;";
		}
		return str;
	}

	/**
	 * Inspection of an effective character string.
	 * null and length judge the character sequence of 0 to be an invalid character sequence.
	 * @param str The character string to verify
	 * @return In the case of an effective character string, it is true.
	 */
	static public boolean validString(String str) {
		if (str == null)
			return false;
		if (str.equals("null"))
			return false;
		if (str.trim().length() == 0)
			return false;
		if (str.equals("　"))
			return false;
		return (0 < str.length());
	}

	/**
	 * Inspection of an invalid character string.
	 * null and length judge the character sequence of 0 to be an invalid character sequence.
	 * @param str The character string to verify
	 * @return In the case of an effective character string, it is true.
	 */
	static public boolean invalidString(String str) {
		return !validString(str);
	}

	/**
	 * Character string comparison
	 * @param before The character for comparison
	 * @param after Standard character
	 * @return 0  is In the case of the same character
	 *           -1 is The character for comparison is null or a meaningless writing character.
	 *           1  is When it differs from a standard character
	 */
	public static int checkString(String before, String after) {
		// The data for updating is checked.
		if (zNull(before).equals("")) {
			return -1;
		} else if (!before.equals(after)) {
			return 1;
		}
		return 0;
	}

	/**
	 * Numerical conversion processing
	 * @param str	Object character string
	 * @return long
	 */
	public static long toLongCase(String str) {
		if (zNull(str).equals("")) {
			return 0;
		}

		Long num = new Long(str);
		return num.longValue();
	}

	/**
	 * Processing which divides param by ":"
	 * @param str	Object character string
	 * @return String
	 */
	public static String toStringTime(String str) {

		if (zNull(str).equals("")) {
			return "";
		} else if (str.length() == 4) {
			return str.substring(0, 2) + ":" + str.substring(2, str.length());
		} else if (str.length() == 6) {
			return str.substring(0, 2) + ":" + str.substring(2, 4) + ":" + str.substring(4, str.length());
		} else {
			return str;
		}

	}

	/**
	 * zZero
	 * @param str
	 * @return After [ String ] edit
	 */
	public static String zZero(long str) {
		if (str == 0) {
			return "";
		}
		return Long.toString(str);
	}

	/**
	 * The specified character string is changed into a half-width character.<BR>
	 *
	 * @param str The character string to change
	 * @return The character string after conversion
	 */
	public static String zen2han(String str) {

		StringBuffer result = new StringBuffer();

		if (z2hTable == null) {
			z2hTableSetup();
		}

		int len = str.length();

		for (int iv = 0; iv < len; iv++) {
			char cv = str.charAt(iv);
			Character co = new Character(cv);
			if (z2hTable.containsKey(co)) {
				result.append(z2hTable.get(co));
			} else {
				result.append(cv);
			}
		}

		return result.toString();
	}

	/**
	 * Rearrangement processing   An object character string is rearranged into the order of reverse.
	 * @param str The character string for rearrangement
	 * @return The character string after rearrangement
	 */
	public static String RearrangementString(String str) {
		StringBuffer sb = new StringBuffer();
		for (int i = str.length(); i >= 1; i--) {
			sb.append(str.substring(i - 1, i));
		}
		return sb.toString();
	}

	/**
	 * Function for FILL   A specification character is buried 
	 * from the left until it becomes the number of specification beams about an object character sequence.
	 * @param type
	 * @param str
	 * @param length
	 */
	public static String StringFill(String type, String str, int length) {

		String wstr = "";
		for (int i = 0; i < length; i++) {
			wstr += type;
		}
		wstr += str;
		return wstr.substring(wstr.length() - length, wstr.length());
	}

	/**
	 * An int type numerical value is changed into a character string.
	 * @param num	Object numerical value
	 * @return Character string
	 */
	public static String toStrCase(int num) {

		String wstr = "";

		wstr = String.valueOf(num);
		return wstr.toString();
	}

	/**
	 * Triple figure comma edit
	 * @param in_val Object number
	 * @return Triple figure comma edit
	 *
	 */
	public static String toNumCommaFormat(long in_val) {
		return toNumCommaFormat(Long.toString(in_val));
	}

	/**
	 * Triple figure comma edit
	 * @param in_val Object number
	 * @return Triple figure comma edit
	 *
	 */
	public static String toNumCommaFormat(BigDecimal in_val) {
		if (in_val == null)
			return "";
		return toNumCommaFormat(in_val.toString());
	}

	/**
	 * Triple figure comma edit
	 * @param in_msg Object number
	 * @return Triple figure comma edit
	 */
	public static String toNumCommaFormat(String in_msg) {

		//Added by zhanjc 2006/02/25 Start
		//「.」123456.789  ->  123456
		int end = in_msg.indexOf(".");
		if (end > 0) {
			in_msg = in_msg.substring(0, end);
		}

		//Added by zhanjc 2006/02/25 End
		if (in_msg == null) {
			return "";
		}
		if (in_msg.length() == 0) {
			return "";
		}

		String ret = "";
		String fugou = "";

		if (!NumberUtil.isNumber(in_msg.charAt(0))) {
			fugou = in_msg.substring(0, 1);
			in_msg = in_msg.substring(1, in_msg.length());
		}

		StringBuffer sb = new StringBuffer(in_msg);
		int msg_len = in_msg.length();
		int comma_num = (int) ((msg_len - 1) / 3);
		int comma_pos = (int) (msg_len % 3);
		if (comma_pos == 0) {
			comma_pos = 3;
		}
		if (comma_num == 0) {
			comma_pos = 0;
		}
		for (int i = 0; i < comma_num; i++) {
			sb.insert(comma_pos + 3 * i + i, ",");
		}

		ret = fugou + sb.toString();

		return ret;
	}

	/**
	 * Unnecessary 0 below a decimal point is deleted.
	 * @param str Object character string
	 * @return After [ processing ] character string
	 */
	public static String toNumZeroDelete(String str) {
		double num = 0;
		String wstr = str;
		try {
			num = Double.parseDouble(str);
			wstr = String.valueOf(num);
			if (wstr.lastIndexOf("0") == (wstr.length() - 1)) {
				wstr = wstr.substring(0, wstr.length() - 2);
			}
		} catch (NumberFormatException e) {
		}
		return wstr;
	}

	/**
	 * Conversion table setup
	 *
	 */
	private static void z2hTableSetup() {
		if (z2hTable != null) {
			return;
		}
		/*
		 z2hTable = new Hashtable();

		 z2hTable.put(new Character('ア'), "ｱ");
		 z2hTable.put(new Character('イ'), "ｲ");
		 z2hTable.put(new Character('ウ'), "ｳ");
		 z2hTable.put(new Character('エ'), "ｴ");
		 z2hTable.put(new Character('オ'), "ｵ");
		 z2hTable.put(new Character('カ'), "ｶ");
		 z2hTable.put(new Character('キ'), "ｷ");
		 z2hTable.put(new Character('ク'), "ｸ");
		 z2hTable.put(new Character('ケ'), "ｹ");
		 z2hTable.put(new Character('コ'), "ｺ");
		 z2hTable.put(new Character('サ'), "ｻ");
		 z2hTable.put(new Character('シ'), "ｼ");
		 z2hTable.put(new Character('ス'), "ｽ");
		 z2hTable.put(new Character('セ'), "ｾ");
		 z2hTable.put(new Character('ソ'), "ｿ");
		 z2hTable.put(new Character('タ'), "ﾀ");
		 z2hTable.put(new Character('チ'), "ﾁ");
		 z2hTable.put(new Character('ツ'), "ﾂ");
		 z2hTable.put(new Character('テ'), "ﾃ");
		 z2hTable.put(new Character('ト'), "ﾄ");
		 z2hTable.put(new Character('ナ'), "ﾅ");
		 z2hTable.put(new Character('ニ'), "ﾆ");
		 z2hTable.put(new Character('ヌ'), "ﾇ");
		 z2hTable.put(new Character('ネ'), "ﾈ");
		 z2hTable.put(new Character('ノ'), "ﾉ");
		 z2hTable.put(new Character('ハ'), "ﾊ");
		 z2hTable.put(new Character('ヒ'), "ﾋ");
		 z2hTable.put(new Character('フ'), "ﾌ");
		 z2hTable.put(new Character('ヘ'), "ﾍ");
		 z2hTable.put(new Character('ホ'), "ﾎ");
		 z2hTable.put(new Character('マ'), "ﾏ");
		 z2hTable.put(new Character('ミ'), "ﾐ");
		 z2hTable.put(new Character('ム'), "ﾑ");
		 z2hTable.put(new Character('メ'), "ﾒ");
		 z2hTable.put(new Character('モ'), "ﾓ");
		 z2hTable.put(new Character('ヤ'), "ﾔ");
		 z2hTable.put(new Character('ユ'), "ﾕ");
		 z2hTable.put(new Character('ヨ'), "ﾖ");
		 z2hTable.put(new Character('ラ'), "ﾗ");
		 z2hTable.put(new Character('リ'), "ﾘ");
		 z2hTable.put(new Character('ル'), "ﾙ");
		 z2hTable.put(new Character('レ'), "ﾚ");
		 z2hTable.put(new Character('ロ'), "ﾛ");
		 z2hTable.put(new Character('ワ'), "ﾜ");
		 z2hTable.put(new Character('ヲ'), "ｦ");
		 z2hTable.put(new Character('ン'), "ﾝ");
		 z2hTable.put(new Character('ガ'), "ｶﾞ");
		 z2hTable.put(new Character('ギ'), "ｷﾞ");
		 z2hTable.put(new Character('グ'), "ｸﾞ");
		 z2hTable.put(new Character('ゲ'), "ｹﾞ");
		 z2hTable.put(new Character('ゴ'), "ｺﾞ");
		 z2hTable.put(new Character('ザ'), "ｻﾞ");
		 z2hTable.put(new Character('ジ'), "ｼﾞ");
		 z2hTable.put(new Character('ズ'), "ｽﾞ");
		 z2hTable.put(new Character('ゼ'), "ｾﾞ");
		 z2hTable.put(new Character('ゾ'), "ｿﾞ");
		 z2hTable.put(new Character('ダ'), "ﾀﾞ");
		 z2hTable.put(new Character('ヂ'), "ﾁﾞ");
		 z2hTable.put(new Character('ヅ'), "ﾂﾞ");
		 z2hTable.put(new Character('デ'), "ﾃﾞ");
		 z2hTable.put(new Character('ド'), "ﾄﾞ");
		 z2hTable.put(new Character('バ'), "ﾊﾞ");
		 z2hTable.put(new Character('ビ'), "ﾋﾞ");
		 z2hTable.put(new Character('ブ'), "ﾌﾞ");
		 z2hTable.put(new Character('ベ'), "ﾍﾞ");
		 z2hTable.put(new Character('ボ'), "ﾎﾞ");
		 z2hTable.put(new Character('パ'), "ﾊﾟ");
		 z2hTable.put(new Character('ピ'), "ﾋﾟ");
		 z2hTable.put(new Character('プ'), "ﾌﾟ");
		 z2hTable.put(new Character('ペ'), "ﾍﾟ");
		 z2hTable.put(new Character('ポ'), "ﾎﾟ");
		 z2hTable.put(new Character('ヴ'), "ｳﾞ");
		 z2hTable.put(new Character('ァ'), "ｧ");
		 z2hTable.put(new Character('ィ'), "ｨ");
		 z2hTable.put(new Character('ゥ'), "ｩ");
		 z2hTable.put(new Character('ェ'), "ｪ");
		 z2hTable.put(new Character('ォ'), "ｫ");
		 z2hTable.put(new Character('ャ'), "ｬ");
		 z2hTable.put(new Character('ュ'), "ｭ");
		 z2hTable.put(new Character('ョ'), "ｮ");
		 z2hTable.put(new Character('ッ'), "ｯ");
		 z2hTable.put(new Character('。'), "｡");
		 z2hTable.put(new Character('、'), "､");
		 z2hTable.put(new Character('ー'), "ｰ");
		 z2hTable.put(new Character('Ａ'), "A");
		 z2hTable.put(new Character('Ｂ'), "B");
		 z2hTable.put(new Character('Ｃ'), "C");
		 z2hTable.put(new Character('Ｄ'), "D");
		 z2hTable.put(new Character('Ｅ'), "E");
		 z2hTable.put(new Character('Ｆ'), "F");
		 z2hTable.put(new Character('Ｇ'), "G");
		 z2hTable.put(new Character('Ｈ'), "H");
		 z2hTable.put(new Character('Ｉ'), "I");
		 z2hTable.put(new Character('Ｊ'), "J");
		 z2hTable.put(new Character('Ｋ'), "K");
		 z2hTable.put(new Character('Ｌ'), "L");
		 z2hTable.put(new Character('Ｍ'), "M");
		 z2hTable.put(new Character('Ｎ'), "N");
		 z2hTable.put(new Character('Ｏ'), "O");
		 z2hTable.put(new Character('Ｐ'), "P");
		 z2hTable.put(new Character('Ｑ'), "Q");
		 z2hTable.put(new Character('Ｒ'), "R");
		 z2hTable.put(new Character('Ｓ'), "S");
		 z2hTable.put(new Character('Ｔ'), "T");
		 z2hTable.put(new Character('Ｕ'), "U");
		 z2hTable.put(new Character('Ｖ'), "V");
		 z2hTable.put(new Character('Ｗ'), "W");
		 z2hTable.put(new Character('Ｘ'), "X");
		 z2hTable.put(new Character('Ｙ'), "Y");
		 z2hTable.put(new Character('Ｚ'), "Z");
		 z2hTable.put(new Character('ａ'), "a");
		 z2hTable.put(new Character('ｂ'), "b");
		 z2hTable.put(new Character('ｃ'), "c");
		 z2hTable.put(new Character('ｄ'), "d");
		 z2hTable.put(new Character('ｅ'), "e");
		 z2hTable.put(new Character('ｆ'), "f");
		 z2hTable.put(new Character('ｇ'), "g");
		 z2hTable.put(new Character('ｈ'), "h");
		 z2hTable.put(new Character('ｉ'), "i");
		 z2hTable.put(new Character('ｊ'), "j");
		 z2hTable.put(new Character('ｋ'), "k");
		 z2hTable.put(new Character('ｌ'), "l");
		 z2hTable.put(new Character('ｍ'), "m");
		 z2hTable.put(new Character('ｎ'), "n");
		 z2hTable.put(new Character('ｏ'), "o");
		 z2hTable.put(new Character('ｐ'), "p");
		 z2hTable.put(new Character('ｑ'), "q");
		 z2hTable.put(new Character('ｒ'), "r");
		 z2hTable.put(new Character('ｓ'), "s");
		 z2hTable.put(new Character('ｔ'), "t");
		 z2hTable.put(new Character('ｕ'), "u");
		 z2hTable.put(new Character('ｖ'), "v");
		 z2hTable.put(new Character('ｗ'), "w");
		 z2hTable.put(new Character('ｘ'), "x");
		 z2hTable.put(new Character('ｙ'), "y");
		 z2hTable.put(new Character('ｚ'), "z");
		 z2hTable.put(new Character('１'), "1");
		 z2hTable.put(new Character('２'), "2");
		 z2hTable.put(new Character('３'), "3");
		 z2hTable.put(new Character('４'), "4");
		 z2hTable.put(new Character('５'), "5");
		 z2hTable.put(new Character('６'), "6");
		 z2hTable.put(new Character('７'), "7");
		 z2hTable.put(new Character('８'), "8");
		 z2hTable.put(new Character('９'), "9");
		 z2hTable.put(new Character('０'), "0");
		 z2hTable.put(new Character('！'), "!");
		 z2hTable.put(new Character('”'), "\"");
		 z2hTable.put(new Character('＃'), "#");
		 z2hTable.put(new Character('＄'), "$");
		 z2hTable.put(new Character('％'), "%");
		 z2hTable.put(new Character('＆'), "&");
		 z2hTable.put(new Character('’'), "'");
		 z2hTable.put(new Character('（'), "(");
		 z2hTable.put(new Character('）'), ")");
		 z2hTable.put(new Character('＝'), "=");
		 z2hTable.put(new Character('～'), "~");
		 z2hTable.put(new Character('｜'), "|");
		 z2hTable.put(new Character('￥'), "\\");
		 z2hTable.put(new Character('＾'), "^");
		 z2hTable.put(new Character('－'), "-");
		 z2hTable.put(new Character('‘'), "`");
		 z2hTable.put(new Character('＠'), "@");
		 z2hTable.put(new Character('｛'), "{");
		 z2hTable.put(new Character('｝'), "}");
		 z2hTable.put(new Character('＋'), "+");
		 z2hTable.put(new Character('；'), ";");
		 z2hTable.put(new Character('：'), ":");
		 z2hTable.put(new Character('＊'), "*");
		 z2hTable.put(new Character('「'), "｢");
		 z2hTable.put(new Character('」'), "｣");
		 z2hTable.put(new Character('［'), "[");
		 z2hTable.put(new Character('］'), "]");
		 z2hTable.put(new Character('＜'), "<");
		 z2hTable.put(new Character('＞'), ">");
		 z2hTable.put(new Character('，'), ",");
		 z2hTable.put(new Character('．'), ".");
		 z2hTable.put(new Character('・'), "･");
		 z2hTable.put(new Character('？'), "?");
		 z2hTable.put(new Character('／'), "/");
		 z2hTable.put(new Character('＿'), "_");

		 z2hTable.put(new Character('　'), " ");
		 z2hTable.put(new Character('゛'), "ﾞ");
		 z2hTable.put(new Character('゜'), "ﾟ");
		 //        z2hTable.put(new Character('＂'), "\"");
		 */
	}

	/**
	 * 右スペス(半角)を削除する
	 * @param val 変換文字列
	 * @return String　変換した文字列
	 */
	public static String rHalfTrim(String val) {
		String valUse = "";
		if (val == null) {
			return "";
		} else {
			//右スペス(半角)を削除した文字列を返す
			int iEnd = val.length();
			for (int i = val.length() - 1; i >= 0; i--) {
				if (val.charAt(i) == ' ') {
					iEnd--;
				} else {
					break;
				}
			}
			valUse = val.substring(0, iEnd);
		}
		return valUse;
	}

	/**
	 * 空白文字を削除する
	 * @param val
	 * @return
	 */
	public static String safeTrim(String val) {
		String val1=zNull(val);
		if (val1 == null)
			return "";
		else
			return val1.trim();
	}

	/**
	 * Convert array to ArrayList, reserving all elements in array
	 * @param objects Object array
	 * @return ArrayList
	 * @author zhanJC
	 */
	public static ArrayList array2ArrayList(Object[] objects) {
		int iCapacity = 0;
		if (objects != null) {
			iCapacity = objects.length;
		} else {
			return null;
		}
		ArrayList arrayObject = new ArrayList(iCapacity);
		if (objects != null) {
			for (int i = 0; i < objects.length; i++) {
				arrayObject.add(objects[i]);
			}
		}
		return arrayObject;
	}

	/**
	 * Convert ArrayList to array, reserving all elements in ArrayList
	 * @param objects ArrayList
	 * @return Object
	 * @author zhanJC
	 */
	public static Object arrayList2Array(List objects, Class classs) throws Exception {
		if (objects == null || objects.size() == 0) {
			return java.lang.reflect.Array.newInstance(classs, 0);
		}
		int capacity = objects.size();
		Object array = java.lang.reflect.Array.newInstance(classs, capacity);
		for (int i = 0; i < capacity; i++) {
			java.lang.reflect.Array.set(array, i, objects.get(i));
		}
		return array;
	}

	/**
	 * 前ゼロを付加した結果を返する
	 *
	 * @param intval intゼロフォーマット対象値
	 * @param length intに変換後、この値をからゼロフォーマット用文字数を生成する
	 * @return String　前ゼロを付加した文字列
	 */
	public static String addPreZero(int intval, int length) {
		return addPreZero((long) intval, length);
	}

	/**
	 * 前ゼロを付加した結果を返する
	 *
	 * @param lngval lngゼロフォーマット対象値
	 * @param length intに変換後、この値をからゼロフォーマット用文字数を生成する
	 * @return String　前ゼロを付加した文字列
	 * 
	 * 2004/01/16 新規作成 ZuoYan
	 */
	public static String addPreZero(long lngval, int length) {
		String result = String.valueOf(lngval);
		String fm = "";
		try {
			int len = length;
			if (len > 0) {
				for (int i = 0; i < len; i++) {
					fm += "0";
				}
				NumberFormat nf = new DecimalFormat(fm);
				result = nf.format(lngval);
			}
		} catch (NumberFormatException ne) {
			return String.valueOf(lngval);
		}
		return result;
	}

	/**
	 * カンマ区切り
	 * input    new String[]{"a","b"}
	 * return   a, b
	 * @param strings
	 * @see  StringUtil#toCommaString(List stringList)
	 * @author zgy
	 * @return
	 */
	public static String toCommaString(String[] strings) {
		return toCommaString(Arrays.asList(strings));
	}

	/** カンマ区切り
	 * 
	 * input    new String[]{"a","b"}
	 * return   a,b
	 * 
	 * Adjacent elements are separated by the characters
	 * <tt>","</tt> (comma).  Elements are converted to strings as
	 * by <tt>String.valueOf(Object)</tt>.<p>
	 * @param stringList
	 * @author zgy
	 * @return
	 */
	public static String toCommaString(List stringList) {
		StringBuffer buf = new StringBuffer();
		Iterator i = stringList.iterator();
		boolean hasNext = i.hasNext();
		while (hasNext) {
			Object o = i.next();
			buf.append(String.valueOf(o));
			hasNext = i.hasNext();
			if (hasNext)
				buf.append(",");
		}
		return buf.toString();
	}

	/**
	 * カンマ区切り
	 * input    new String[]{"a","b"}
	 * return   'a', 'b'
	 * @param strings
	 * @see  StringUtil#toCommaString(List stringList)
	 * @author HuMing
	 * @return
	 */
	public static String toCommaStringSql(String[] strings) {
		if(strings==null)
			return "";
		return toCommaStringSql(Arrays.asList(strings));
	}

	/** カンマ区切り
	 * 
	 * input    new List has "a","b"
	 * return   'a','b'
	 * 
	 * Adjacent elements are separated by the characters
	 * <tt>","</tt> (comma).  Elements are converted to strings as
	 * by <tt>String.valueOf(Object)</tt>.<p>
	 * @param stringList
	 * @author HuMing
	 * @return
	 */
	public static String toCommaStringSql(List stringList) {
		StringBuffer buf = new StringBuffer();
		Iterator i = stringList.iterator();
		boolean hasNext = i.hasNext();
		while (hasNext) {
			Object o = i.next();
			buf.append("'");
			buf.append(String.valueOf(o));
			buf.append("'");
			hasNext = i.hasNext();
			if (hasNext)
				buf.append(",");
		}
		return buf.toString();
	}

	/**
	 * 文字の並べた交替した結果を返する
	 *
	 * @param sVal フォーマット値
	 * @param oldVal 交替する文字は並べる
	 * @param repVal 交替した文字が並べることに用いる
	 * @return String 文字の並べた交替した結果を返する
	 */
	public static String replace(String sVal, String oldVal, String repVal) {
		if (sVal == null || oldVal == null || repVal == null) {
			return sVal;
		}
		StringBuffer result = new StringBuffer(sVal);
		String str = new String(sVal);

		int preLength = 0;
		int newEndPost = 0;
		while (str.toString().indexOf(oldVal) >= 0) {
			int startPost = preLength + str.toString().indexOf(oldVal);
			int endPost = startPost + oldVal.length();

			result.delete(startPost, endPost);
			result.insert(startPost, repVal);

			newEndPost = startPost + repVal.length();
			str = result.substring(newEndPost, result.length());
			preLength = newEndPost;
		}
		return result.toString();
	}

	/**
	 * To compute the absolute URL by relative URL,such as http://192.168.0.45/election/....
	 * @param request	HttpServletRequest
	 * @return	String
	 * @author zhanjc
	 */
	public static String getAbsoluteUrl(HttpServletRequest request, String relateiveURL) {
		String strPath = "";
		String strPrj = request.getContextPath();
		String strContext = request.getRequestURL().toString();
		int iStart = strContext.indexOf(strPrj, strContext.indexOf("//") + 3) + strPrj.length();
		relateiveURL = relateiveURL.trim();
		if (!relateiveURL.startsWith("/") && !relateiveURL.startsWith("\\")) {
			relateiveURL = "/" + relateiveURL;
		}
		return strContext.substring(0, iStart) + relateiveURL;
	}
	/**
	 * 多余的字符串打上...
	 * @param str
	 * @param sublength
	 */
	public static String toSubDot(String str,int sublength){
		String s=StringUtil.safeTrim(str);
		if(s.length()>sublength){
			s=s.substring(0,sublength)+"...";
		}
		return s;
	}
	/**
	 * 传入的值是list<map>这种形式的
	 * 对某一个key裁减
	 * @param r list
	 * @param title 要减的key
	 * @param count 长度
	 */
	public static void cutAndAddDotListMap(List r,String title,int count) {
		if(r==null)
			return;
		for(int i=0;i<r.size();i++){
			Map m=(Map)r.get(i);
			String ss=StringUtil.toSubDot(m.get(title)+"",count);
			m.put(title, ss);
		}
	}

	/**
	 * 判断字符串是否有意义，不为空或""
	 * 
	 * @param s
	 * @return false代表为空或""，true代表有意义
	 */
	public static boolean iszNull(String s) {
		s=StringUtil.safeTrim(s);
		if ("".equals(s)) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 判断对象是否有意义，不为空或""
	 *
	 * @param s
	 * @return false代表为空或""，true代表有意义
	 */
	public static boolean iszNull(Object s) {
		String ss=StringUtil.zNull(s);
		ss=StringUtil.safeTrim(ss);
		if ("".equals(ss)) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 判断字符串空变成0
	 * @param s
	 * @return 报错返回0
	 */
	public static String tozZero(String s) {
		if("".equals(zNull(s))){
			return "0";
		}
		try{
			Double.parseDouble(s);
		}catch(Exception e){
			return "0";
		}	
		return s;
	}
	/**
	 * 一个空变成json的{}
	 * 以Json转换时报错
	 * @param s
	 * @return
	 */
	public static String zNullToJsonNull(String s){
		String ss=zNull(s);
		if("".equals(ss)){
			return "{}";
		}else{
			return s;
		}
	}

	/**
	 * 是不是手机号码
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^1[3|4|5|8|7][0-9]\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	/**
	 * 对象转成url字符串
	 * @param o
	 * @return
	 * @throws Exception
	 */
	public static String parseURLPair(Object o){
		try {
			Class<? extends Object> c = o.getClass();
			Field[] fields = c.getDeclaredFields();
			Map<String, Object> map = new TreeMap<String, Object>();
			for (Field field : fields) {
				field.setAccessible(true);
				String name = field.getName();
				Object value = field.get(o);
				if (value != null)
					map.put(name, value);
			}
			Set<Map.Entry<String, Object>> set = map.entrySet();
			Iterator<Map.Entry<String, Object>> it = set.iterator();
			StringBuffer sb = new StringBuffer();
			while (it.hasNext()) {
				Map.Entry<String, Object> e = it.next();
				sb.append(e.getKey()).append("=").append(e.getValue()).append("&");
			}
			return sb.deleteCharAt(sb.length()-1).toString();
		}catch (Exception e){
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 大写字母变成下划线+小写
	 * @param param
	 * @return
	 */
	public static String camel4underline(String param){
		Pattern p=Pattern.compile("[A-Z]");
		if(param==null ||param.equals("")){
			return "";
		}
		StringBuilder builder=new StringBuilder(param);
		Matcher mc=p.matcher(param);
		int i=0;
		while(mc.find()){
			builder.replace(mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase());
			i++;
		}

		if('_' == builder.charAt(0)){
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	public static void main(String[] args) {
		System.out.println(StringUtil.camel4underline("createTime"));
	}
}
